package co.za.itweb.itwebafrica;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedHashMap;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

public class LazyLoadImage extends AsyncTask<String, Void, Bitmap> {
	/*
     * Code related to cache
     */
    private static final int HARD_CACHE_CAPACITY = 100;
    // Hard cache, with a fixed maximum capacity and a life duration
    @SuppressWarnings("rawtypes")
	private final static HashMap sHardBitmapCache = new LinkedHashMap(
            HARD_CACHE_CAPACITY / 2);
    private String url;
    private final WeakReference<ImageView> imageViewReference;

    
    public LazyLoadImage(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        url = params[0];
        try {
        	Bitmap bitmap = BitmapFactory.decodeStream(new URL(url).openConnection()
                    .getInputStream());
        	 addBitmapToCache(url, bitmap);
        	 
        	 if (isCancelled()) {
        		 return null;
         	}
            return bitmap;
            
           
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        
    	
       
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (isCancelled()) {
            result = null;
        }
        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(result);
                
            }
        }
    }

    @Override
    protected void onPreExecute() {
        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageResource(R.drawable.stub);
            }
        }
    }
    
    
    @SuppressWarnings("unchecked")
	public void addBitmapToCache(String url, Bitmap bitmap) {
        if (bitmap != null) {
            synchronized (sHardBitmapCache) {
                try {
                    sHardBitmapCache.put(url, bitmap);
                } catch (OutOfMemoryError ex) {
                    // Oops, out of memory, clear cache
                    clearCache();
                }
            }
        }
    }
 
    @SuppressWarnings("unchecked")
	public Bitmap getBitmapFromCache(String url) {
        if (url == null) {
            return null;
        }
        // First try the hard reference cache
        synchronized (sHardBitmapCache) {
            final Bitmap bitmap = (Bitmap) sHardBitmapCache.get(url);
            if (bitmap != null) {
                // Bitmap found in hard cache
                // Move element to first position, so that it is removed last
                sHardBitmapCache.remove(url);
                sHardBitmapCache.put(url, bitmap);
                return bitmap;
            }
        }
        return null;
    }
 
    public void clearCache() {
        sHardBitmapCache.clear();
    }
 
    @SuppressWarnings("rawtypes")
	public static HashMap getShardbitmapcache() {
        return sHardBitmapCache;
    }
    
    
}

